<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>数字人面试官管理</span>
      </div>
      <div style="margin-left: 130px">
        <el-upload
            class="upload-demo"
            drag
            action="http://localhost:8781/file/uploadToOss"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
  </div>
      <el-form :model="form" :rules="rules" ref="form" label-width="120px" style="width: 500px">
        <el-form-item label="形象图链接" prop="imageLink" >
          <el-input v-model="form.image" readonly></el-input>
        </el-form-item>
        <el-form-item label="面试官名称" prop="interviewerName">
          <el-input v-model="form.aiName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="resetForm">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column  label="数字人形象">
          <template slot-scope="scope">
            <img :src="scope.row.image" alt="Interviewer Image" style="max-width: 100px; max-height: 100px;">
          </template>

        </el-table-column>
        <el-table-column prop="aiName" label="面试官名称"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updatedTime" label="更新时间"></el-table-column>

      </el-table>
      <div v-if="tableData.length === 0" style="text-align: center;">暂无数据</div>

      <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>

import axios from "axios";
export default {
  data() {
    return {
      imageUrl: null,
      form: {
        image: '',
        aiName: ''
      },
      pageForm:{
        pageSize:10,
        pageNum:1
      },
      tableData:[],
      total:0,
      pageSize: 10,
          currentPage: 1



    };
  },
  mounted() {
    // 模拟从后端获取数据
   this.fetchInterViewerList();
  }
,

  methods: {

    handleCurrentChange(page) {
      this.pageForm.pageNum = page;
      // 这里可以添加获取数据的方法，例如从API获取数据
      this.fetchInterViewerList();
    },



    fetchInterViewerList() {
      // 这里可以发送Axios请求获取数据，根据currentPage和pageSize来获取分页数据
      // 假设这里有一个名为fetchData的方法来获取数据
      axios.post('http://localhost:8781/interviewer/listInterviewers', this.pageForm).then(res=>{
        console.log(res)
        this.tableData = res.data.data.records;
        console.log(res.data.total)
        this.total = res.data.data.total;
      })
    }

    ,

    handleAvatarSuccess(res, file) {
      console.log(res)
      console.log(file)
      this.form.image = res.data;
    },

    handleAvatarChange(file, fileList) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG &&!isPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      if (isJPG || isPNG && isLt2M) {
        const reader = new FileReader();
        reader.readAsDataURL(file.raw);
        reader.onload = (e) => {
          this.imageUrl = e.target.result;
        };
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG &&!isPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG || isPNG && isLt2M;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 这里可以添加提交表单数据的逻辑，例如发送AJAX请求
          console.log('表单提交成功', this.form);

          axios.post('http://localhost:8781/interviewer/addInterviewer', this.form).then(res=>{
            console.log(res)
            if(res.data.code==200){
              this.$message.success(res.data.msg);

            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    }
  }
};
</script>

<style scoped>
.avatar-uploader.icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
          }
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.box-card {
         margin-bottom: 20px;
       }
</style>